<template>
<div>
  <div style="margin: 10px 0">
    <el-button plain type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
    <el-button plain type="danger" @click="deleteBatch" v-if="tableData.length !== 0">批量删除 <i class="el-icon-remove-outline"></i></el-button>
  </div>
  <el-empty description="暂无预约" v-if="tableData.length === 0"></el-empty>
  <div v-else>
    <el-table :data="tableData" border stripe header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="80" v-if="false"></el-table-column>
      <el-table-column type="index"
                       :index="this.indexStartNum"
                       label="序号"
                       width="100">
      </el-table-column>
      <el-table-column prop="date" label="日期"></el-table-column>
      <el-table-column prop="timePeriod" label="时段"></el-table-column>
      <el-table-column prop="num" label="人数"></el-table-column>
      <el-table-column prop="count" label="预约人数"></el-table-column>
      <el-table-column label="操作" width="180px">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleEdit(scope.row)" plain>编辑 <i class="el-icon-edit"></i></el-button>
          <el-button type="danger" @click="del(scope.row)" plain>删除 <i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
    <el-dialog title="值班信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="80px" size="small">
        <el-form-item label="日期">
          <el-date-picker v-model="form.date" value-format="yyyy-MM-dd" type="date" autocomplete="off"></el-date-picker>
        </el-form-item>
        <el-form-item label="时段">
          <el-radio-group v-model="form.timePeriod">
            <el-radio label="上午" value="上午"></el-radio>
            <el-radio label="下午" value="下午"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="人数">
          <el-input type="number" min="1" max="30" v-model="form.num" style="width: 60px"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button style="margin-left: 45%;margin-top: 15px;" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


</div>
</template>

<script>

export default {
  name: "Stage",
  data() {
    return {
      indexStartNum: 1,
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      phy: localStorage.getItem("phy") ? JSON.parse(localStorage.getItem("phy")) : {},
      dialogFormVisible: false,
      form: {},
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    //请求分页查询数据
    this.load()
  },
  methods: {
    load() {
      this.request.get("/stage/findStage", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: this.phy.id
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
      })
    },
    save() {
      console.log(this.form)
      if (this.form.num < this.form.count){
        this.$message.error("排班人数少于目前问诊人数！")
      }else {
        this.form.phyId = this.phy.id
        console.log(this.phy.id)
        this.request.post("/stage/edit", this.form).then(res => {
          if (res) {
            this.$message.success("保存成功")
            this.dialogFormVisible = false
            this.load()
          } else {
            this.$message.error("保存失败")
          }
        })
      }
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    del(row) {
      if (row.count > 0){
        this.$message.error("已有问诊信息，无法删除排班！")
      }else {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.request.delete("/stage/del?id=" + row.id).then(res => {
            if (res) {
              this.$message.success("删除成功")
              this.load()
            } else {
              this.$message.error("删除失败")
            }
          })
        });
      }
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id) //将对象数组变成纯id数组 [{},{},{}] => [1,2,3]
      this.$confirm('此操作将永久批量删除选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/stage/delBatch", {data: ids}).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      });
    },
    reset() {
      // this.title = ""
      // this.author = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    },
  }
}
</script>

<style scoped>

</style>
